package com.nlscan.qdkit.ui.publico.art.mvp;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.qing.mvpart.base.QvFragment;
import com.qing.mvpart.mvp.IPresenter;
import com.qing.mvpart.mvp.IView;
import com.quvii.qvlib.util.LogUtil;
import com.quvii.qvlib.util.QvToastUtil;

/**
 * 基于MVP封装UI相关的 Fragment 基类
 * Created by QING on 2023-02-11.
 * -- 增加沉浸式
 */
public abstract class BaseMvpFragment<T extends ViewBinding, P extends IPresenter> extends QvFragment<P> implements IView {

    @Nullable
    protected T binding;

    protected abstract T getViewBinding(@NonNull LayoutInflater inflater, @NonNull ViewGroup container);

    public boolean checkBindingFail() {
        boolean ret = binding == null;
        if (ret) {
            LogUtil.i("check view binding fail");
        }

        return ret;
    }

    @Override
    public void showError(int code, String msg) {
//        QvToastUtil.showS(msg + "\n" + QvSdkErrorUtil.getSdkResult(code));
    }

    @Override
    public boolean useButterKnife() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void showResult(int code) {
//        showMessage(QvSdkErrorUtil.getSdkResult(code));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = getViewBinding(inflater, container);
        rootView = binding.getRoot();
//        RouterCenter.INSTANCE.inject(this);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        SystemUtil.setForceDarkNotAllowed(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    /**
     * 沉浸式处理相关 BEGIN
     */


    /**
     * 沉浸式处理相关 END
     */

    /***************************************************
     *    工具相关方法封装 BEGIN
     ****************************************************/

    public boolean isEmpty(Object obj) {
        if (obj == null) {
            throw new NullPointerException();
        }
        return TextUtils.isEmpty(getText(obj));
    }

    public boolean isEmpty(String str) {
        return TextUtils.isEmpty(str);
    }

    /**
     * 获取 editTest，textView,checkbox和button的文字
     */
    public String getText(Object obj) {
        if (obj instanceof TextView) {
            return ((TextView) obj).getText().toString();
        }
        LogUtil.e("类型转换异常");
        return "";
    }

    public void toast(String msg) {
        QvToastUtil.showS(msg);
    }

    public void toast(int strId) {
        QvToastUtil.showS(strId);
    }

    protected LayoutInflater getInflater() {
        return getContext() == null ? null : (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    protected void hideSoftInput() {
        if (getActivity() == null) {
            return;
        }
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    /***************************************************
     *    工具相关方法封装 END
     ****************************************************/
}
